js_ffi
this library creates a bridge to javascript in the browser at runtime using web assembly
A foreign function interface(FFI) library for invoking Javascript functions from Web Assembly for many programming languages
- no code generation or special cargo components
- support for callbacks (e.g.
setTimeout
) - futures based on callbacks
- memory as a parameter
- wrapper library for Rust
- works with C or C++, check out examples here
- typed arrays
- can be executed in a web worker
This project has similarities to Javascript's <function>.call(<object>,a0,a1,...)
but with the limitations of Web Assembly's function call restrictions.
Hello World! in Rust
note js_ffi is language agnostic, I just used Rust as example because I like it
[]
= "0.6"
use *;
# cli commands for building web assembly
:
:
:
Drawing
See demo here
use *;
Event Listener
use *;
Async Example
Using an executor
library we can easily turn callbacks into futures and run behavior asynchronously.
use *;
Third Party
Wrap third party libraries. Anything function in global space should be able to be wrapped and invoked.
use *;
Standard Web Libraries
A collection of libraries exist that expose javascript functionality so you don't have to implement it yourself. Just add them to your project and go!
How it works
- Get a handle to some Javascript function using
register_function
. Re-use this handle as often as possible. - If you are invoking this function as a regular function, use the appropriate
invoke_*
function based on the number of arguments you are passing (invoke_1
,invoke_7
,etc.). - If you are invoking this function as a method of an object represented by a
JSValue
, use the appropriatecall_*
function based on the number of arguments you are passing (call_1
,invoke_7
,etc.) and make sure your object is the first paramter.
Don't like Rust?
The script js_ffi.js
has nothing Rust specific.
- Operations execute through an interface specified in this
js_ffi.h
js_ffi
expects an entry pointmain()
- If you plan on having your module receive data it must implement
jsffimalloc(i32) -> i32
- If you plan on having your module receive callbacks it must implement
jsfficallback(i32,f32,f32,f32,f32,f32,f32,f32,f32,f32,f32)
- strings are simply c-strings in memory that end in a
0
character.
Run as a webworker
// main.js
let w = ;
w.;
// worker.js
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in js_ffi
by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.